package com.danger.View.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;

import com.danger.Activity.R;
import com.danger.Model.bean.ObjectInfo;
import com.danger.Util.ImageLoaderUtil;
import com.nostra13.universalimageloader.core.assist.ImageSize;

import java.util.List;

/**
 * Created by danger on 2016/3/30.
 */
public class MyAdapter extends BaseAdapter {

    private List<ObjectInfo> list;
    private LayoutInflater inflater;
    private Context context;
    private ImageSize imageSize = new ImageSize(120, 130);

    public MyAdapter(List<ObjectInfo> list, Context context) {
        this.list = list;
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_listview_item, null);
            holder = new Holder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        ObjectInfo object = list.get(position);
        holder.item_title_textView.setText(object.getTitle());
        holder.item_category_textView.setText(object.getCategory());
        holder.item_describe_textView.setText(object.getDescribe());
        holder.item_issuer_textView.setText(object.getName());
        holder.item_time_textView.setText(object.getCreatedAt());
        holder.item_address_textView.setText(object.getAddress());
        String url = object.getPhoto().toString();
        if (url == "" || url == null) {
            holder.item_photo_imageView.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.no_photo));
        } else {
            new ImageLoaderUtil(imageSize, holder.item_photo_imageView, url, context).load();
        }
        return convertView;
    }

    class Holder {

        private ImageView item_photo_imageView;
        private TextView item_title_textView;
        private TextView item_category_textView;
        private TextView item_describe_textView;
        private TextView item_issuer_textView;
        private TextView item_time_textView;
        private TextView item_address_textView;

        public Holder(View view) {
            item_photo_imageView = (ImageView) view.findViewById(R.id.item_photo_imageView);
            item_title_textView = (TextView) view.findViewById(R.id.item_title_textView);
            item_category_textView = (TextView) view.findViewById(R.id.item_category_textView);
            item_describe_textView = (TextView) view.findViewById(R.id.item_describe_textView);
            item_issuer_textView = (TextView) view.findViewById(R.id.item_issuer_textView);
            item_time_textView = (TextView) view.findViewById(R.id.item_time_textView);
            item_address_textView = (TextView) view.findViewById(R.id.item_address_textView);
        }
    }
}
